package com.leetcode.algorithm.y22.m06;

/**
 * 557. 反转字符串中的单词 III
 * 
 * https://leetcode.cn/problems/reverse-words-in-a-string-iii/
 * 
 * @author jie.deng
 *
 */
public class Question0557Solution02 {

	public String reverseWordsIII(String s) {
		StringBuilder builder = new StringBuilder(s.length());
		StringBuilder tmp = new StringBuilder();
		int len = s.length();
		int idx = 0;
		while (idx < len) {
			char ch = s.charAt(idx);
			if (ch != ' ') {
				tmp.append(ch);
			} else {
				builder.append(tmp.reverse());
				tmp = new StringBuilder();
				builder.append(' ');
			}
			idx++;
		}
		builder.append(tmp.reverse());
		return builder.toString();
	}
}
